package com.hopu.mvc.controller;

import com.hopu.mvc.entity.User;
import com.hopu.mvc.entity.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 */
@Controller
public class UserController {

    /**
     * 返回登录页面
     * @return
     */
    @RequestMapping("login.html")
    public String toLogin(){
        return "login";
    }

    /**
     * 1. 以参数名(name=username)获得参数
     * @return
     */
    @RequestMapping(value = "login1",method = RequestMethod.POST)
    public String login1(String username, String password, Model model){
        System.out.println("username="+username);
        System.out.println("password="+password);
        if("admin".equals(username) && "123".equals(password)){
            //跳转到用户列表页面 请求转发 forward:xxx  重定向 redirect:xxx
            return "redirect:findAll";
        }else{
            //添加错误信息，跳转登录页面
            model.addAttribute("error","账号或密码错误");
            return "login";
        }
    }

    @GetMapping("findAll")
    public String findAll(Model model){
        //模拟数据
        List<User> users = Arrays.asList(new User("aaa","111"),
                new User("bbb","111"),
                new User("ccc","111"),
                new User("ddd","111"));
        model.addAttribute("users",users);
        return "users";
    }


    /**
     * 2. 给参数配置@RequestParam注解获得参数
     * @return
     */
    @RequestMapping(value = "login2",method = RequestMethod.POST)
    public String login2(@RequestParam("username") String uname,
                         @RequestParam("password") String pwd){
        System.out.println("username="+uname);
        System.out.println("password="+pwd);
        //需要跳转的页面名称
        return "index";
    }

    /**
     * 3. 通过对象获得参数
     * @param user
     * @return
     */
    @PostMapping("login3")
    public String login3(User user){
        System.out.println(user);
        return "index";
    }

    /**
     * 4. 通过请求对象获得
     * @param request
     * @return
     */
    @PostMapping("login4")
    public String login4(HttpServletRequest request){
        System.out.println("username:" +request.getParameter("username"));
        System.out.println("password:" +request.getParameter("password"));
        return "index";
    }

    /**
     * 5. 通过路径传参
     * http://localhost:8080/login5/xxx/yyy
     */
    @GetMapping("login5/{user}/{pwd}")
    public String login5(@PathVariable("user") String username,
                         @PathVariable("pwd") String password){
        System.out.println("username="+username);
        System.out.println("password="+password);
        return "index";
    }

    @PostMapping("login6")
    public String login6(UserVO user){
        System.out.println(user.getUser().get(0));
        System.out.println(user.getUser().get(1));
        return "index";
    }

}
